package org.mantenimientojsf.persistencia;

import org.mantenimientojsf.beans.Solicitudes;
import java.sql.Date; 
import org.mantenimientojsf.beans.*;
import org.mantenimientojsf.controller.*;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
 import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.faces.context.FacesContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.el.ELContext;
import javax.faces.model.SelectItem;
import org.mantenimientojsf.generales.DataGManager;
import org.mantenimientojsf.generales.JSFUtil;


/**
*
* @author
*/

@ManagedBean
@SessionScoped
public class SolicitudesPersistencia implements Serializable {
private static final long serialVersionUID = 1L;
public static Exception excepcion;
public static boolean esMensaje = false;
public static Connection con = null;
static DataGManager dataGManager;
static List<Solicitudes> list;

public SolicitudesPersistencia(){
     ELContext elContext = FacesContext.getCurrentInstance().getELContext();
     dataGManager =(DataGManager) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null,"dataGManager");
}


/**
*
* insert()
*/

public static boolean insert (Solicitudes solicitudes ) {
  excepcion = null;
  try {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
   
    estatus.setIdestatus(solicitudes.getIdestatus());
   
      if(!estatusController.exists(estatus)){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecordcodetable") + " estatus");
           esMensaje = true;
           return false;
      }

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pinsert = null;
      String sql = "insert into solicitudes(idsolicitud,cedula,fechahora,motivo,idestatus)  values(?,?,?,?,?)";
      pinsert = con.prepareStatement(sql);
              pinsert.setInt(1,solicitudes.getIdsolicitud());
        pinsert.setString(2,solicitudes.getCedula());
        pinsert.setDate(3,solicitudes.getFechahora());
        pinsert.setString(4,solicitudes.getMotivo());
        pinsert.setString(5,solicitudes.getIdestatus());
      int filas= pinsert.executeUpdate();
      pinsert.close();
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
 }


/**
*
* update()
*/

public static boolean update(Solicitudes solicitudes ) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pupdate = null;
      String sql = "update solicitudes set  idsolicitud=?, cedula=?, fechahora=?, motivo=?, idestatus=?  where  idsolicitud ='"+solicitudes.getIdsolicitud()+"'  ";
      pupdate= con.prepareStatement(sql);
      pupdate.setInt(1,solicitudes.getIdsolicitud());
pupdate.setString(2,solicitudes.getCedula());
pupdate.setDate(3,solicitudes.getFechahora());
pupdate.setString(4,solicitudes.getMotivo());
pupdate.setString(5,solicitudes.getIdestatus());
      int filas= pupdate.executeUpdate();
      pupdate.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noupdate"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* update()
*/

public static boolean update(String sqlupdate ) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pupdate = null;
      String sql = "update  solicitudes set " + sqlupdate +"";
      pupdate= con.prepareStatement(sql);
      int filas= pupdate.executeUpdate();
      pupdate.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noupdate"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* updatePrimaryKey()
*/

public static boolean updatePrimaryKey(Solicitudes solicitudes_old , Solicitudes solicitudes) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pupdate = null;
      String sql = "update solicitudes set  idsolicitud=?  where  idsolicitud ='"+solicitudes_old.getIdsolicitud()+"'  ";
      pupdate= con.prepareStatement(sql);
      pupdate.setInt(1,solicitudes.getIdsolicitud());
      int filas= pupdate.executeUpdate();
      pupdate.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noupdate"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* delete()
*/

public static boolean delete(Solicitudes solicitudes ) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pdelete = null;
      String sql = "delete from solicitudes  where  idsolicitud = ?  ";
      pdelete= con.prepareStatement(sql);
      pdelete.setInt(1,solicitudes.getIdsolicitud());
      int filas= pdelete.executeUpdate();
      pdelete.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nodelete"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* delete()
*/

public static boolean delete(String sqldelete ) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pdelete = null;
      String sql = "delete from solicitudes where " + sqldelete +"";
      pdelete= con.prepareStatement(sql);
      int filas= pdelete.executeUpdate();
      pdelete.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nodelete"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* deleteAll()
*/

public static boolean deleteAll() {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      PreparedStatement pdelete = null;
      String sql = "delete from solicitudes  ";
      pdelete= con.prepareStatement(sql);
      int filas= pdelete.executeUpdate();
      pdelete.close();
      if (filas <= 0){
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nodeleteall"));
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* exists()
*/



public static boolean exists(Solicitudes solicitudes) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return false;
  }
      String sql = "select * from solicitudes where idsolicitud='"+solicitudes.getIdsolicitud()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return false;
   }
    rs.close();
    psquery.close();
 return true;
} catch (Exception ex) {
      excepcion = ex;
}
  return false;
}


/**
*
* findByPrimaryKey()
*/



public static Solicitudes findByPrimaryKey(Solicitudes solicitudes) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
      String sql = "select * from solicitudes where idsolicitud='"+solicitudes.getIdsolicitud()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
   estatus2 =  estatusController.findByPrimaryKey(estatus);
   if (estatus2== null) {
       //no existe registro con ese codigo 
   } else {
   estatus= estatus2;
   solicitudes.setEstatus(estatus);
     }
            solicitudes.setIdsolicitud(rs.getInt("idsolicitud"));
solicitudes.setCedula(rs.getString("cedula"));
solicitudes.setFechahora(rs.getDate("fechahora"));
solicitudes.setMotivo(rs.getString("motivo"));
solicitudes.setIdestatus(rs.getString("idestatus"));
    }
    rs.close();
    psquery.close();
 return solicitudes;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByIdsolicitud()
*/

public static List<Solicitudes> findByIdsolicitud(Solicitudes solicitudes,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
      String sql = "select * from solicitudes where idsolicitud='"+solicitudes.getIdsolicitud()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByCedula()
*/

public static List<Solicitudes> findByCedula(Solicitudes solicitudes,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
      String sql = "select * from solicitudes where cedula='"+solicitudes.getCedula()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByFechahora()
*/

public static List<Solicitudes> findByFechahora(Solicitudes solicitudes,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
      String sql = "select * from solicitudes where fechahora='"+solicitudes.getFechahora()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByMotivo()
*/

public static List<Solicitudes> findByMotivo(Solicitudes solicitudes,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
      String sql = "select * from solicitudes where motivo='"+solicitudes.getMotivo()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByIdestatus()
*/

public static List<Solicitudes> findByIdestatus(Solicitudes solicitudes,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
      String sql = "select * from solicitudes where idestatus='"+solicitudes.getIdestatus()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* locateByParameters()
*/

public static List<Solicitudes> locateByParameters(Map parametros,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
       list = new ArrayList<>();
  String sql = "select * from solicitudes";
  if (parametros != null) {
      Iterator it = parametros.entrySet().iterator();
      StringBuilder sb = new StringBuilder("");
      int i = 0;
      while (it.hasNext()) {
          if (i != 0){
            sb.append(" and ");
         }
         i++;
        Map.Entry e = (Map.Entry) it.next();
        sb.append(e.getKey() +"= '"+  e.getValue()+"'");
      }
      if (!sb.toString().equals("")){
         sql = sql + " where " + sb.toString();
      }
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* Double fFunc(String funcion, String campo)
*/



public static Double func(String funcion, String campo) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return 0.0;
  }
      String sql = "select "+funcion+"("+campo+") as contador from solicitudes";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return 0.0;
   }
    Double tmp =0.0;
   rs.beforeFirst();
   while (rs.next()) {
    tmp =  rs.getDouble("contador");
    }
    rs.close();
    psquery.close();
 return tmp;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0.0;
}


/**
*
* Double func(String funcion, String campo,String where)
*/



public static Double func(String funcion, String campo, String where) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return 0.0;
  }
      String sql = "select "+funcion+"("+campo+") as contador from solicitudes where "+where +" ";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return 0.0;
   }
    Double tmp =0.0;
   rs.beforeFirst();
   while (rs.next()) {
    tmp =  rs.getDouble("contador");
    }
    rs.close();
    psquery.close();
 return tmp;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0.0;
}


/**
*
* count()
*/



public static Integer count() {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return 0;
  }
      String sql = "select count(*) as contador from solicitudes";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return 0;
   }
   rs.beforeFirst();
   while (rs.next()) {
    x =  rs.getInt("contador");
    }
    rs.close();
    psquery.close();
 return x;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0;
}


/**
*
* count(String sqlquery)
*/



public static Integer count(String sqlquery) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return 0;
  }
      String sql = "select count(*) as contador from solicitudes where  " + sqlquery +"";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return 0;
   }
   rs.beforeFirst();
   while (rs.next()) {
    x =  rs.getInt("contador");
    }
    rs.close();
    psquery.close();
 return x;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0;
}


/**
*
* query()
*/

public static List<Solicitudes> query(String query,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
        list = new ArrayList<>();
  String sql = "select * from solicitudes where " + query +"";
  if (query == null || query.equals("")) {
  return null;
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* getListRecords()
*/

public static List<Solicitudes> getListRecords(Map parametros,boolean withRelation) {
  excepcion = null;
  try {

  con= dataGManager.getCon();
  if (con == null) {
      JSFUtil.addSuccessMessage(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.noconecction"));
      return null;
  }
       list = new ArrayList<>();
  String sql = "select * from solicitudes";
  if (parametros != null) {
      Iterator it = parametros.entrySet().iterator();
      StringBuilder sb = new StringBuilder("");
      while (it.hasNext()) {
        Map.Entry e = (Map.Entry) it.next();
        sb.append(e.getKey() +"="+  e.getValue());
      }
      if (!sb.toString().equals("")){
         sql = sql + " where " + sb.toString();
      }
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
          excepcion = new Exception(ResourceBundle.getBundle("org/mantenimientojsf/properties/mensajes").getString("msg.nofoundrecords") + " solicitudes");
      rs.close();
      psquery.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
       
Estatus estatus = new Estatus();
Estatus estatus2 = new Estatus();
EstatusController estatusController = new EstatusController();
        Solicitudes solicitudes2 = new Solicitudes(
         rs.getInt("idsolicitud"),rs.getString("cedula"),rs.getDate("fechahora"),rs.getString("motivo"),rs.getString("idestatus"));
            
    estatus.setIdestatus(rs.getString("idestatus"));
            
  if(withRelation){
     estatus2 =  estatusController.findByPrimaryKey(estatus);
     if (estatus2== null) {
       //not found records 
     } else {
     estatus= estatus2;
     solicitudes2.setEstatus(estatus);
       }
   }
       list.add(solicitudes2);
    }
    rs.close();
    psquery.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
 }


}
